/*
 * Copyright (C) 2004, Velocityme Partnership
 * PermissionCache.java
 *
 * Created on 14 September 2004, 08:12
 */

package com.velocityme.utility;

import com.velocityme.enums.Permission;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author  Robert
 */
public class PermissionCache {
    
    static private PermissionCache m_instance = null;
    
    private Map<Long,Map<Long,Set<Permission>>> m_userTable = null;
    
    /** Creates a new instance of PermissionCache */
    private PermissionCache() {
        m_userTable = new HashMap<Long,Map<Long,Set<Permission>>>();
    }
    
    public static PermissionCache getInstance() {
        if (m_instance == null)
            m_instance = new PermissionCache();
        return m_instance;
    }
    
    public Set<Permission> get(Long userId, Long nodeId) {
        Map<Long,Set<Permission>> nodeTable = m_userTable.get(userId);
        if (nodeTable == null)
            return null;
        return nodeTable.get(nodeId);
    }
    
    public void set(Long userId, Long nodeId, Set<Permission> permissions) {
        Map<Long,Set<Permission>> nodeTable = m_userTable.get(userId);
        if (nodeTable == null) {
            nodeTable = new HashMap<Long,Set<Permission>>();
            m_userTable.put(userId, nodeTable);
        }
        nodeTable.put(nodeId, permissions);
    }
    
    public void clearUser(Long userId) {
        m_userTable.remove(userId);
    }
    
    public void clear() {
        m_userTable.clear();
    }
    
}
